library(shiny)
library(tidyverse)

# Load precomputed data

sct_data <- readRDS(file = "data/rds/proj.rds")
sct_data_b <- readRDS(file = "data/rds/categories.rds")
choropleth_data <- readRDS(file = "data/rds/choropleth_data.rds")
bar_data  <- readRDS(file = "data/rds/bar_data.rds")
radar_data  <- readRDS(file = "data/rds/radar_data.rds")
tree_data <- readRDS(file = "data/rds/tree_data.rds")
parset_data <- read_csv2("data/parset_data.csv")
treemap_data <- readRDS(file = "data/rds/treemap_data.rds")

heatmap_2012_data <- readRDS(file = "data/rds/heatmap_2012_data.rds")
heatmap_2013_data <- readRDS(file = "data/rds/heatmap_2013_data.rds")
heatmap_2014_data <- readRDS(file = "data/rds/heatmap_2014_data.rds")
heatmap_2015_data <- readRDS(file = "data/rds/heatmap_2015_data.rds")
heatmap_2016_data <- readRDS(file = "data/rds/heatmap_2016_data.rds")
heatmap_2017_data <- readRDS(file = "data/rds/heatmap_2017_data.rds")

choropleth_UF2012_data <- readRDS(file = "data/rds/percentage_UF2012.rds")
choropleth_UF2013_data <- readRDS(file = "data/rds/percentage_UF2013.rds")
choropleth_UF2014_data <- readRDS(file = "data/rds/percentage_UF2014.rds")
choropleth_UF2015_data <- readRDS(file = "data/rds/percentage_UF2015.rds")
choropleth_UF2016_data <- readRDS(file = "data/rds/percentage_UF2016.rds")
choropleth_UF2017_data <- readRDS(file = "data/rds/percentage_UF2017.rds")

choropleth_meso2012_data <- readRDS(file = "data/rds/percentage_meso2012.rds")
choropleth_meso2013_data <- readRDS(file = "data/rds/percentage_meso2013.rds")
choropleth_meso2014_data <- readRDS(file = "data/rds/percentage_meso2014.rds")
choropleth_meso2015_data <- readRDS(file = "data/rds/percentage_meso2015.rds")
choropleth_meso2016_data <- readRDS(file = "data/rds/percentage_meso2016.rds")
choropleth_meso2017_data <- readRDS(file = "data/rds/percentage_meso2017.rds")

server <- function(input, output, session) {

    observe({

        session$sendCustomMessage(type="scatterplot_values",
                                  message = list(sct_data, sct_data_b))

    })

    observe({

        session$sendCustomMessage(type="parset_values",
                                  message = c(treemap_data,
                                              list(parset_data[-c(1,3,4,5,6)])))

    })

    observe({

        session$sendCustomMessage(type="dendrogram_values",
                                  message = list(tree_data,
                                                 bar_data,
                                                 radar_data))

    })

    observe({

        session$sendCustomMessage(type="choropleth_values",
                                  message = list(
                                      list(choropleth_UF2012_data,
                                           choropleth_UF2013_data,
                                           choropleth_UF2014_data,
                                           choropleth_UF2015_data,
                                           choropleth_UF2016_data,
                                           choropleth_UF2017_data),
                                      list(heatmap_2012_data,
                                           heatmap_2013_data,
                                           heatmap_2014_data,
                                           heatmap_2015_data,
                                           heatmap_2016_data,
                                           heatmap_2017_data),
                                      list(choropleth_meso2012_data,
                                           choropleth_meso2013_data,
                                           choropleth_meso2014_data,
                                           choropleth_meso2015_data,
                                           choropleth_meso2016_data,
                                           choropleth_meso2017_data))
                                  )

    })

# Comunication with parallel sets
    observe({
        req(input$scope)
        req(input$name)

        s <- input$scope
        v <- input$name

        switch(
            s,
            "pais"= {
                filtered <- filter(parset_data, pais  == v)[-c(1,3,4,5,6)]
            },
            "regiao"= {
                filtered <- filter(parset_data, regiao == v)[-c(1,2,4,5,6)]
            },
            "uf"= {
                filtered <- filter(parset_data, uf == v)[-c(1,2,3,5,6)]
            },
            "mesorregiao"= {
                filtered <- filter(parset_data, mesorregiao == v)[-c(1,2,3,4,6)]
            },
            "microrregiao"= {
                filtered <- filter(parset_data, microrregiao == v)[-c(1,2,3,4,5)]
            },
            #there is no way to reach this level for now
            "municipio"= {
                filtered <- filter(parset_data, municipio == v)[-c(1,2,3,4,5)]
            }
        )

        session$sendCustomMessage(type="treemap_values_change", message = filtered)

    })

}
